<?php
// +----------------------------------------------------------------------
// | quanli team
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2020 http://www.fistforward.com/ All rights reserved.
// +----------------------------------------------------------------------
// | Author: create by allen.qiang
// +----------------------------------------------------------------------

/**
 * 呼叫中心客服工单记录类
 * @package  module/base/bill
 * @author   create by allen.qiang
 * @version 1.0
 */

class callcenterWorkOrder {

    private $dbca;

    public function __construct( &$dbca ) {
        $this->dbca = $dbca;
    }

    /**
     * 查询呼叫中心客服工单记录表信息
     * @param array $arg_array                          查询参数数组
     * @return null|array
     */
    public function searchWorkOrder (&$arg_array) {

        //sql字符串
        $sql = '';

        //参数类型字符串
        $params_type = '';

        //参数值数组
        $params_array = array();

        //工单记录流水号
        if (isset($arg_array['work_no']) && is_numeric($arg_array['work_no'])) {
            $sql .= ' AND work_no = ?';
            $params_type .= 's';
            $params_array[] = trim($arg_array['work_no']);
        }

        //创建人
        if (isset($arg_array['creator']) && is_numeric($arg_array['creator'])) {
            $sql .= ' AND creator LIKE ?';
            $params_type .= 's';
            $params_array[] = '%'.trim($arg_array['creator']).'%';
        }

        //工单类型
        if (isset($arg_array['work_type_id']) && !empty($arg_array['work_type_id'])) {
            $sql .= ' AND work_type_id = ?';
            $params_type .= 's';
            $params_array[] = trim($arg_array['work_type_id']);
        }

        //类型内容
        if (isset($arg_array['work_type_content_id']) && !empty($arg_array['work_type_content_id'])) {
            $sql .= ' AND work_type_content_id = ?';
            $params_type .= 's';
            $params_array[] = trim($arg_array['work_type_content_id']);
        }

        //会员名
        if (isset($arg_array['pcustID']) && !empty($arg_array['pcustID']) ) {
            $sql .= ' AND pcustID LIKE ?';
            $params_type .= 's';
            $params_array[] = "%".trim($arg_array['pcustID'])."%";
        }

        if (isset($arg_array['extend']) && !empty($arg_array['extend'])) {
            $sql .= $arg_array['extend'];
        }

        if ($sql) {
            $sql = ' WHERE ' . substr($sql, 4);
        }

        try {

            if (isset($arg_array['rp']) && $arg_array['rp'] > 0) {

                //统计数量
                $search_sql = 'SELECT COUNT(*) AS item_count FROM callcenter_work_order ' . $sql;
                $count_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);
                $item_count = $count_array[0]->item_count;
                if ($item_count == 0) {
                    $arg_array['count'] = 0;
                    return null;
                }

                $arg_array['count'] = $item_count;

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

                //分页
                if (!isset($arg_array['page']) || !is_numeric($arg_array['page']) || $arg_array['page'] <= 0)
                    $arg_array['page'] = 1;

                $sql .= sprintf(' LIMIT %d, %d', ($arg_array['page'] - 1) * $arg_array['rp'], $arg_array['rp']);

            } else {

                //排序
                if (isset($arg_array['sortname']) && !empty($arg_array['sortname']))
                    $sql .= sprintf(' ORDER BY %s %s', $arg_array['sortname'], $arg_array['sortorder']);

            }

            //查询分类信息
            $search_sql = 'SELECT * FROM callcenter_work_order ' . $sql;
            $temp_item_array = &$this->dbca->stmt_query($search_sql, $params_type, $params_array);

            if (!$temp_item_array) {
                return null;
            }

            foreach ($temp_item_array as &$item) {

                $item_array[] = array(
                    'uuid' => $item->uuid,
                    'timeStamp' => $item->timeStamp,
                    'creator' => $item->creator,
                    'createTime' => $item->createTime,
                    'work_no' => $item->work_no,
                    'pcustID' => $item->pcustID,
                    'work_type_id' => $item->work_type_id,
                    'work_type_name' => $item->work_type_name,
                    'work_type_content_id' => $item->work_type_content_id,
                    'work_type_content_name' => $item->work_type_content_name,
                    'remark' => $item->remark
                );
            }

            return $item_array;

        } catch (Exception $e) {

            error_log($e->getMessage());
            return null;
        }

    }

    /**
     * 保存保存
     * @param array $arg_array 参数数组
     * @return array
     */
    public function saveCallcenterWorkOrder( $arg_array ) {

        //加载类文件
        quanli_require('module/base/system/sysItemMultilevel.class.php');

        if (!isset($arg_array['pcustID'], $arg_array['second_id'])) {
            return array( 'code' => 400, 'message' => '参数错误');
        }

        if (empty($arg_array['second_id'])) {
            return array( 'code' => 400, 'message' => '请选择工单类型');
        }

        try {

            //查询选项信息
            $sys_item_multilevel = new entity('sys_item_multilevel');
            $sys_item_multilevel->item_id = $arg_array['second_id'];
            if (!$this->dbca->fetch($sys_item_multilevel))
                throw new Exception('工单类型没找到', 404);

            $work_type_name = $sys_item_multilevel->item_name;

            if(!empty($arg_array['third_id'])){
                $item_multilevel = new entity('sys_item_multilevel');
                $item_multilevel->item_id = $arg_array['third_id'];
                if(!$this->dbca->fetch($item_multilevel)){
                    throw new Exception('工单类型内容没找到', 404);
                }
                $work_type_content_name = $item_multilevel->item_name;
            }else{
                $work_type_content_name = "";
            }

            //查询会员信息
            $cust_master = new entity('cust_master');
            $cust_master->pcustID = $arg_array['pcustID'];
            if(!$this->dbca->fetch($cust_master)){
                throw new Exception('会员信息不存在', 404);
            }

            //保存数据

            $callcenter_work_order = new entity('callcenter_work_order');
            $callcenter_work_order->work_no = generate_seqno('seqno_work_order');
            $callcenter_work_order->creator = $arg_array['creator'];
            $callcenter_work_order->pcustID = $arg_array['pcustID'];
            $callcenter_work_order->work_type_id = $arg_array['second_id'];
            $callcenter_work_order->work_type_name = $work_type_name;
            $callcenter_work_order->work_type_content_id = $arg_array['third_id'] != "" ? $arg_array['third_id'] : "";
            $callcenter_work_order->work_type_content_name = $work_type_content_name;
            $callcenter_work_order->remark = $arg_array['description'];

            $this->dbca->save($callcenter_work_order);

            $search_son = $this->dbca->stmt_query('SELECT count(*) as num FROM callcenter_work_order WHERE pcustID = ?','i',$arg_array['pcustID']);

            if($search_son[0]->num > 1){
                $hasNum = 1;
            }else{
                $hasNum = 0;
            }

            $result_array['createTime'] = $callcenter_work_order->createTime;
            $result_array['creator'] = $callcenter_work_order->creator;
            $result_array['work_type_name'] = $callcenter_work_order->work_type_name;
            $result_array['work_type_content_name'] = $callcenter_work_order->work_type_content_name;
            $result_array['remark'] = $callcenter_work_order->remark;
            $result_array['hasNum'] = $hasNum;

            return array( 'code' => 200, 'message' => '保存成功', 'data' => $result_array);

        } catch (Exception $e) {

            if ($e->getCode() > 0) {
                return array( 'code' => $e->getCode(), 'message' => $e->getMessage() );
            } else {
                error_log($e->getMessage());
                return array( 'code' => 500, 'message' => '系统错误' );
            }

        }

    }

}